package movenment;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Highscore extends JPanel {
	static final String NAME = "Name";
	static final String HTML = "<html><body>";
	static final String HTMLE ="</body></html>";
	Image image;
	JPanel highScore;
	String score;
	JTextField text;
	FileWork2 fw;
	JButton button;
	JButton menuButton;
	
	
	/*
	 * Creates the highscore panel with a given listener.
	 * @param action gives the commit button this actionlistener.
	 */
	public Highscore(ActionListener action){
		//setSize(630, 470);
		fw = new FileWork2();
		createFrame(action);
		
		setVisible(true);
	}
	
	/*
	 * Fills the Highscore panel with the highscores,
	 * commit field and buttons.
	 * @param action gives the commit button this actionlistener.
	 */
	private void createFrame(ActionListener action){
		setLayout(new BorderLayout(6, 6));
		
		JPanel bottom = new JPanel();
		bottom.setLayout(new FlowLayout());
		text = new JTextField(NAME, 20);
		
		
		FocusListener l = new FocusListener(){

			@Override
			public void focusGained(FocusEvent e) {
				text.setText("");
				
			}

			@Override
			public void focusLost(FocusEvent e) {
				if(text.getText().equals("")){
					text.setText(NAME);
				}
			}
		};
		text.addFocusListener(l);
		bottom.add(text);
		button = new JButton("Commit");
		ActionListener al = new ActionListener(){
			public void actionPerformed(ActionEvent e) { commit(); }

			private void commit() {
				String name = text.getText();
				
				name = name.replaceAll(" ", "");
				fw.writeBasicDetails(name, score);
				button.setEnabled(false);
				newHighScore();
				//revalidate();
				
			}
		};
		
		button.addActionListener(al);
		//button.setAlignmentX(Component.RIGHT_ALIGNMENT);
		menuButton = new JButton("Menu");
		menuButton.addActionListener(action);
		
		
		bottom.add(button);
		bottom.add(menuButton);
		
		add(bottom, BorderLayout.SOUTH);
		
		//addHighScore();
		highScore = new JPanel(new BorderLayout());
		JLabel top = new JLabel("Highscore", JLabel.CENTER);
		top.setAlignmentX(Component.CENTER_ALIGNMENT);
		highScore.setAlignmentX(Component.CENTER_ALIGNMENT);
		highScore.add(top, BorderLayout.NORTH);
		
		JLabel names = new JLabel(HTML + fw.getNames()+ HTMLE, JLabel.CENTER);
		
		String scoreTxt = HTML + fw.getScore() + HTMLE;
		//highScore.setLayout(new BorderLayout());
		JLabel score = new JLabel(scoreTxt);
		score.setMinimumSize(new Dimension(200,1));
		score.setPreferredSize(new Dimension(200,100));
		highScore.add(names, BorderLayout.CENTER);
		highScore.add(score, BorderLayout.LINE_END);
		
		add(highScore, BorderLayout.CENTER);
		//add(new BackgroundPanel());
        //add(new JTextField("TextField",20));
		
		try
		{
			ImageIcon ii = new ImageIcon("desk00.jpg");
			image = ii.getImage();
	      //image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("desk00.jpg"), "Test.jpg"));
	    }
	    catch (Exception e) { /*handled in paintComponent()*/ }
	}
	
	private void newHighScore(){
		remove(highScore);
		highScore = new JPanel(new BorderLayout());
		JLabel top = new JLabel("Highscore", JLabel.CENTER);
		top.setAlignmentX(Component.CENTER_ALIGNMENT);
		highScore.setAlignmentX(Component.CENTER_ALIGNMENT);
		highScore.add(top, BorderLayout.NORTH);
		
		JLabel names = new JLabel(HTML + fw.getNames()+ HTMLE, JLabel.CENTER);
		String scoreTxt = HTML + fw.getScore() + HTMLE;
		JLabel score = new JLabel(scoreTxt);
		score.setMinimumSize(new Dimension(200,1));
		score.setPreferredSize(new Dimension(200,100));
		highScore.add(names, BorderLayout.CENTER);
		highScore.add(score, BorderLayout.LINE_END);
		
		add(highScore, BorderLayout.CENTER);
		validate();
	}
	
//	public void paintComponent( Graphics g )
//	{
//		super.paintComponent( g );
//		Graphics2D g2d = (Graphics2D) g;
//		
//	    g2d.drawImage(image, 10, 10, null); 
//	}
	public void setHighscore(int score){
		this.score = ""+score;
	}
	public void editable(boolean b){
		button.setEnabled(b);
	}
}
